/*
 * @Author: hejian@myhexin.com 
 * @Date: 2019-01-02 10:13:35 
 * @Last Modified by: hejian@myhexin.com
 * @Last Modified time: 2019-02-26 14:53:56
 */
$(function () {
	var vueInit = null;
	vueInit = new Vue({
		mixins: [meter],
		el: "#vuewrap",
		data: {
			androidApp: getPlatform() == 'gphone' ? true : false,
			dpr: $('html').data('dpr'),
			boardData: {
				one: {
					stock: []
				},
				two: {
					stock: []
				},
				three: {
					stock: []
				},
				lowest: [],
				highest: []
			},
			headerData: {

			},
			brokenLineData: {
				startTime: "",
				endTime: "",
				arr: []
			},
			allStockArr: [],
		},
		filters:{
			//用于数值或隐式数值类型
			textFilter:function (val,typeTag) {
				if(isNaN(val) || (!val && val != '0')){
					if(val && val != 'NaN'){
						return val
					}else{
						return '--'
					}
				}else{
					if(!typeTag){
						return val
					}else{
						return val + typeTag
					}
				}
			}
		},
		methods: {
			more: function (from) {
				$('.' + from).find('.more').hide();
				$('.' + from).find('ul li').show();
				thsStats.clickStat("todayhq", from + ".open")
			},
			initChart: function (a, b, c) {
				var option, left;
				var aArr = a;
				var bArr = b;
				var cArr = c;
				var dpr = this.dpr;
				var fontSize = dpr * 11;
				var lineWidth = dpr * 1.8;
				if (dpr == 1) {
					left = "40px";
				} else {
					left = dpr * 35 + 'px';
				}
				option = {
					grid: [{
						left: left,
						right: '1%',
						top: '2%',
						bottom: '6%'
					}],
					series: [{
							type: 'line',
							data: aArr,
							line: {
								show: true,
								style: {
									normal: {
										stroke: '#fea31e',
										lineWidth: lineWidth
									}
								}
							}
						},
						{
							type: 'line',
							data: bArr,
							line: {
								show: true,
								style: {
									normal: {
										stroke: '#4691ee',
										lineWidth: lineWidth
									}
								}
							}
						},
						{
							type: 'line',
							data: cArr,
							line: {
								show: true,
								style: {
									normal: {
										stroke: '#e93030',
										lineWidth: lineWidth
									}
								}
							}
						}
					],
					axis: [{
							position: 'bottom',
							type: 'point',
							data: aArr,
							barGap: '0%',
							tick: {
								show: false
							},
							line: {
								show: false
							},
							label: {
								show: false
							}
						},
						{
							position: 'left',
							type: 'linear',
							xOrY: 'y',
							min: 0,
							splitNumber: 3,
							splitLine: {
								show: true,
							},
							tick: {
								show: false
							},
							line: {
								show: false
							},
							label: {
								show: true,
								style: {
									fontSize: fontSize,
									fill: "#999"
								},
								formatter: function (d) {
									if (d == 1) {
										return '100%'
									} else {
										return d * 100 + '%';
									}
								}
							}
						}
					]
				}
				var myChart = D3Charts.init('chart')
				myChart.setOption(option)
			},
			allRequestData: function () {
				var that = this;

				function header() {
					const result = (localStorage.getItem('LIMITUP__LIMITSCORE__') && JSON.parse(localStorage.getItem('LIMITUP__LIMITSCORE__'))) || {};
					that.headerData['continue'] = result.continue;
					that.headerData['up'] = result.up;
					that.headerData['value'] = result.value;
					that.headerData['y'] = result.y;
					that.headerData['zf'] = result.zf;
					$("#vuewrap").append('<div id="canvas"></div>')
					that.initMeter(that.headerData['value']);
				}

				function board() {
					ajax(hxmConfig.yesterdayAndTodayApi, {}, function (res) {
						if (res.errorcode == 0) {
							that.boardData.one = res.result.one;
							that.boardData.two = res.result.two;
							that.boardData.three = res.result.three;
							that.boardData.lowest = res.result.lowest;
							that.boardData.highest = res.result.highest;
							var arr1 = that.boardData.one.stock || [];
							var arr2 = that.boardData.two.stock || [];
							var arr3 = that.boardData.three.stock || [];
							var arr4 = that.boardData.lowest || [];
							var arr5 = that.boardData.highest || [];
							var len = [arr1.length, arr2.length, arr3.length, arr4.length, arr5.length]
							var inter = []
							var s = 0;
							var e = arr1.length;
							for (var i = 0; i < len.length; i++) {
								var obj = {}
								if (i == 0) {
									obj['s'] = s;
									obj['e'] = e;
								} else {
									s = s + len[i - 1]
									e = e + len[i]
									obj['s'] = s;
									obj['e'] = e;
								}
								inter.push(obj)
							}
							that.allStockArr = that.allStockArr.concat(arr1).concat(arr2).concat(arr3).concat(arr4).concat(arr5);
							$('#initLoading').addClass('hide');
							if (that.allStockArr.length != 0) {
								that.calc(inter);
							}
						}
					}, function (err) {
						$('#initLoading').addClass('hide');
						$('#errLoading').removeClass('hide');
					})
				}

				function brokenLine() {

					ajax(hxmConfig.brokenLineApi, {}, function (res) {
						if (res.errorcode == 0) {
							var len = res.result.length;
							var unit = 100;
							var startTime = Jen.transNumToDate(res.result[0].date);
							var endTime = Jen.transNumToDate(res.result[len - 1].date);
							var fbl = [];
							var gkl = [];
							var cgl = [];
							for (var i = 0; i < len; i++) {
								fbl.push(1 - (res.result[i].zbl / unit))
								gkl.push(res.result[i].dbgkl / unit)
								cgl.push(res.result[i].dbcgl / unit)
							}
							that.brokenLineData.startTime = startTime;
							that.brokenLineData.endTime = endTime;
							that.brokenLineData.arr = res.result;
							setTimeout(function () {
								that.initChart(fbl, gkl, cgl);
							}, 50)
						}
					}, function (err) {})
				}
				return {
					header: header,
					board: board,
					brokenLine: brokenLine
				}
			},
			calc: function (inter) {
				var that = this;
				if (getAppVersion()) {
					var stockcode1 = [];
					var marketcode1 = [];
					for (var a = 0; a < that.allStockArr.length; a++) {
						stockcode1.push(that.allStockArr[a].stockcode)
						marketcode1.push(that.allStockArr[a].stockmarket)
					}
					__getMarketInfo(['10', '34818', '48'], stockcode1, marketcode1, function (data) {
						var upRise = data['34818'];
						for (var a = 0; a < that.allStockArr.length; a++) {
							that.allStockArr[a]["upRise"] = upRise[a] == "--" ? upRise[a] : parseFloat(upRise[a].split("%")[0])
						}
						that.boardData.one.stock = that.allStockArr.slice(inter[0].s, inter[0].e)
						that.boardData.one.stock.length != 0 && 	that.boardData.one.stock.sort(compare('upRise', 1, 'stockcode'))

						that.boardData.two.stock = that.allStockArr.slice(inter[1].s, inter[1].e)
						that.boardData.two.stock.length != 0 && 	that.boardData.two.stock.sort(compare('upRise', 1, 'stockcode'))

						that.boardData.three.stock = that.allStockArr.slice(inter[2].s, inter[2].e)
						that.boardData.three.stock.length != 0 && 	that.boardData.three.stock.sort(compare('upRise', 1, 'stockcode'))

						that.boardData.lowest = that.allStockArr.slice(inter[3].s, inter[3].e)
						that.boardData.highest = that.allStockArr.slice(inter[4].s, inter[4].e)
					})
				} else {
					for (var a = 0; a < that.allStockArr.length; a++) {
						that.allStockArr[a]["upRise"] = "0.00%"
					}
				}
			},
			reload: function () {
				this.allRequestData().board();
				this.allRequestData().brokenLine();
				$('#errLoading').addClass('hide');
				$('#initLoading').removeClass('hide');
			},
			// 监听滚动事件
			listenScroll: function () {
				var that = this;
				var dpr = $("html").data("dpr") ? $("html").data("dpr") : 1;
				var bottomt1 = true;
				// 滚动到底部埋点
				$(window).on('scroll', function () {
					var windowHeight =
						getPlatform() == "iphone" ?
						window.screen.height :
						$(window).height(); //window高度
					var totalHeight = $(document).height() / dpr; //页面总高度
					var scrollHeight = $(window).scrollTop() / dpr; //window距顶部距离
					if (
						(scrollHeight + 10 >= totalHeight - windowHeight) &&
						scrollHeight != 0
					) {
						if (bottomt1) {
							thsStats.clickStat("todayhq", "bottom");
							bottomt1 = false;
						}
					}
				})
			},
			// 跳K线
			jumpK: function (txt, item, index) {
				var code = item.stockcode;
				var marketcode = item.stockmarket;
				thsStats.jumpNativeStat('todayhq', txt + "." + (index + 1), '2216', {
					targid: "stock_" + code
				});
				var url = "client.html?action=ymtz^stockcode=" + code + "^marketid=" + marketcode + "^webid=2216"
				setTimeout(function () {
					location.href = url;
				}, 20)
			}
		},
		created: function () {
			$('#initLoading').removeClass('hide');
		},
		mounted: function () {
			thsStats.pageStat('todayhq')
			var that = this;
			setTimeout(function () {
				that.listenScroll();
				that.allRequestData().board();
				that.allRequestData().header();
				that.allRequestData().brokenLine();
			}, 50)
		}
	})
})